//
//  ExtractRecordsController.swift
//  SCA
//
//  Created by tjun on 2025/9/14.
//

import UIKit
import SnapKit

class ExtractRecordsController: BaseController {
    
    private var scrollView: UIScrollView!
    private let overviewButton = SelectableGradientLineButton()
    private let powerListButton = SelectableGradientLineButton()
    var segmentedControl: UISegmentedControl!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBar.title = "提取记录".localizedString
        
        let items = ["个人领取".localizedString, "团队领取".localizedString]
        segmentedControl = UISegmentedControl(items: items)
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.backgroundColor = UIColor(white: 0.2, alpha: 1.0)
        segmentedControl.selectedSegmentTintColor = .black
        segmentedControl.addTarget(self, action: #selector(segmentedChanged(_:)), for: .valueChanged)
        segmentedControl.setTitleTextAttributes([
            .foregroundColor: UIColor.gray,
            .font: UIFont.systemFont(ofSize: 14, weight: .medium)
        ], for: .normal)
        
        segmentedControl.setTitleTextAttributes([
            .foregroundColor: UIColor.white,
            .font: UIFont.systemFont(ofSize: 14, weight: .medium)
        ], for: .selected)
        
        segmentedControl.layer.cornerRadius = 8
        segmentedControl.layer.masksToBounds = true
        view.addSubview(segmentedControl)
        segmentedControl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(navBarHeight + 4)
            make.height.equalTo(36)
            make.width.equalTo(200)
        }
  
        scrollView = UIScrollView(frame: CGRect(x: 0, y: 44 + navBarHeight, width: view.width, height: screenHeight - navBarHeight - 44)).addTo(view)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.contentInsetAdjustmentBehavior = .never
        scrollView.isPagingEnabled = true
        scrollView.delegate = self
        scrollView.addTo(view)
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 2, scrollView.frame.size.height)
        
        let personalRecordsController = ExtractRecordsListController(type: 0)
        addChild(personalRecordsController)
        scrollView.addSubview(personalRecordsController.view)
        personalRecordsController.view.frame = CGRectMake(0, 0, scrollView.width, scrollView.height)
        
        let powerListController = ExtractRecordsListController(type: 1)
        addChild(powerListController)
        scrollView.addSubview(powerListController.view)
        powerListController.view.frame = CGRectMake(scrollView.width, 0, scrollView.width, scrollView.height)

    }
 
    @objc private func segmentedChanged(_ sender: UISegmentedControl) {
         print("当前选中索引：\(sender.selectedSegmentIndex)")
         print("当前选中标题：\(sender.titleForSegment(at: sender.selectedSegmentIndex) ?? "")")
        self.scrollView.contentOffset = CGPointMake(CGFloat(sender.selectedSegmentIndex) * self.scrollView.width, 0)
     }
}


extension ExtractRecordsController: UIScrollViewDelegate {
   func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
       let selectedIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width)
       printf("selectedIndex=\(selectedIndex)")
       segmentedControl.selectedSegmentIndex = selectedIndex
   }
   
}

